//
//  BDAddressTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/2/20.
//  Copyright © 2017年 niusaibing. All rights reserved.
//

#import "BDAddressTableViewCell.h"
#import "BDUserAddressModel.h"

@interface BDAddressTableViewCell ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *morenButton;
@property (nonatomic, strong) UIButton *deletedButton;
@property (nonatomic, strong) UIButton *editButton;

@end

@implementation BDAddressTableViewCell
- (void)configeWithModel:(id)model morenBlock:(void(^)())morenBlock editBlock:(void(^)())editBlock deletedBlock:(void(^)())deletedBlock{
    BDUserAddressModel *addressModel = model;
    self.nameLabel.text = addressModel.name;
    self.phoneLabel.text = addressModel.mobile;
    self.addressLabel.text = [addressModel.cityStr stringByAppendingString:addressModel.address];
    self.morenButton.selected = addressModel.isDefault;
    [[self.morenButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (morenBlock) {
            morenBlock();
        }
    }];
    [[self.editButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (editBlock) {
            editBlock();
        }
    }];
    [[self.deletedButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (deletedBlock) {
            deletedBlock();
        }
    }];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    self.nameLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    [self.nameLabel sizeToFit];
    self.nameLabel.left = 10;
    self.nameLabel.top = 10;
    
    self.phoneLabel.font = [UIFont systemFontOfSize:14];
    self.phoneLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    [self.phoneLabel sizeToFit];
    self.phoneLabel.right = self.width - 10;
    self.phoneLabel.top = 10;
    
    self.addressLabel.font = [UIFont systemFontOfSize:14];
    self.addressLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    self.addressLabel.numberOfLines = 0;
    self.addressLabel.width = self.width - 20;
    [self.addressLabel sizeToFit];
    self.addressLabel.left = 10;
    self.addressLabel.top = self.nameLabel.bottom + 10;
    
    self.lineView.backgroundColor = kBDColorGray;
    self.lineView.size = CGSizeMake(self.width, 0.5);
    self.lineView.top = self.addressLabel.bottom + 10;
    
    self.morenButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.morenButton setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
    [self.morenButton setTitle:ASLocalizedString(@"设为默认")forState:UIControlStateNormal];
    [self.morenButton setImage:[UIImage imageNamed:@"address_xuanze"] forState:UIControlStateNormal];
    [self.morenButton setImage:[UIImage imageNamed:@"address_moren"] forState:UIControlStateSelected];
    self.morenButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);
    [self.morenButton sizeToFit];
    self.morenButton.height = 40;
    self.morenButton.top = self.lineView.bottom;
    self.morenButton.left = 10;
    
    self.deletedButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.deletedButton setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
    [self.deletedButton setTitle:ASLocalizedString(@"删除")forState:UIControlStateNormal];
    [self.deletedButton setImage:[UIImage imageNamed:@"address_shanchu"] forState:UIControlStateNormal];
    self.deletedButton.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, -3);
    [self.deletedButton sizeToFit];
    self.deletedButton.height = 40;
    self.deletedButton.top = self.lineView.bottom;
    self.deletedButton.right = self.width - 10;
    
    self.editButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.editButton setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
    [self.editButton setTitle:ASLocalizedString(@"编辑")forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:@"address_bianji"] forState:UIControlStateNormal];
    self.editButton.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, -3);
    [self.editButton sizeToFit];
    self.editButton.height = 40;
    self.editButton.top = self.lineView.bottom;
    self.editButton.right = self.deletedButton.left - 10;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        self.nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel{
    if (!_phoneLabel) {
        self.phoneLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_phoneLabel];
    }
    return _phoneLabel;
}

- (UILabel *)addressLabel{
    if (!_addressLabel) {
        self.addressLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_addressLabel];
    }
    return _addressLabel;
}

- (UIView *)lineView{
    if (!_lineView) {
        self.lineView = [[UIView alloc] init];
        [self.contentView addSubview:_lineView];
    }
    return _lineView;
}

- (UIButton *)morenButton{
    if (!_morenButton) {
        self.morenButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_morenButton];
    }
    return _morenButton;
}

- (UIButton *)editButton{
    if (!_editButton) {
        self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_editButton];
    }
    return _editButton;
}

- (UIButton *)deletedButton{
    if (!_deletedButton) {
        self.deletedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_deletedButton];
    }
    return _deletedButton;
}

@end
